package com.tysong.qq.patterns.impl;

import com.alibaba.fastjson.JSONObject;
import com.tysong.qq.patterns.Strategy;
import com.tysong.qq.service.Daka;
import com.tysong.qq.utils.HttpClientUtil;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 策略模式打卡实现类
 * @Project: qq
 * @Author: Sunday
 * @Create: 2021-09-01 20:52
 **/

@Component("daka")
public class OperationClockIn implements Strategy {

    @Override
    public void doOperation(GroupMsg msg, MsgSender sender, String groupNumber, List<String> listName) {
        String[] originalData = msg.getOriginalData().split(", ");
        String s1 = originalData[1];
        String[] split1 = s1.split("=");
        String name = split1[1];
        System.out.println("name: " + name);
        String msg1 = Daka.INSTANCE.msg(name);
        // 掉接口影响速率因此去除
        // String city = "武汉";
        // String weather = TianQiService.INSTANCE.getWeather(city);
        listName.add(msg1);
        long count = listName.stream().distinct().count();
        long size = listName.size();
        if (size == count) {
            sender.SENDER.sendGroupMsg(groupNumber, msg1);
            // sender.SENDER.sendGroupMsg(groupNumber, weather);
        } else {
            sender.SENDER.sendGroupMsg(groupNumber, "不要重复打卡");
        }
    }

    @Override
    public void neteaseCloudMusic(MsgSender sender, String groupNumber, String keyword) throws Exception {
        String url = "https://api.xingzhige.com/API/wyy/api.php?msg="+keyword+"&n=1&type=json";
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent","Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36");
        String resJson = HttpClientUtil.get(url, headers, 5000, 5000);
        resJson = resJson.substring("json:".length());
        JSONObject jsonObject = JSONObject.parseObject(resJson).getJSONObject("meta").getJSONObject("music");
        String musicUrl = jsonObject.getString("musicUrl");
         sender.SENDER.sendGroupMsg(groupNumber, musicUrl);
    }
}
